package com.lyc.tokendemo.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisCache {
    @Autowired
    public RedisTemplate redisTemplate;

    public <T> void setCacheObject(String key, T value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public <T> void setCacheObject(String key, T value,
                                   Long expireTime, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, expireTime, timeUnit);
    }

    public boolean expire(String key, long timeout) {
        return expire(key, timeout, TimeUnit.SECONDS);
    }

    public boolean expire(String key, long timeout, TimeUnit unit) {
        return redisTemplate.expire(key, timeout, unit);
    }

    public <T> T getCacheObject(String key) {
        ValueOperations<String, T> operations = redisTemplate.opsForValue();
        return operations.get(key);
    }

    public boolean deleteObject(String key) {
        return redisTemplate.delete(key);
    }

    public long deleteObject(Collection collection) {
        return redisTemplate.delete(collection);
    }

    public <T> long setCacheList(String key, List<T> list) {
        Long count = redisTemplate.opsForList().rightPushAll(key, list);
        return count == null ? 0 : count;
    }

    public <T> List<T> getCacheList(String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> set){
        BoundSetOperations<String, T> setOperations = redisTemplate.boundSetOps(key);
        for (T t : set) {
            setOperations.add(t);
        }
        return setOperations;
    }

    public <T> Set<T> getCacheSet(String key){
        return redisTemplate.opsForSet().members(key);
    }

    public <T> void setCacheMap(String key, Map<String, T> map) {
        if (map != null) {
            redisTemplate.opsForHash().putAll(key, map);
        }
    }


    public <T> Map<String, T> getCacheMap(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public <T> void setCacheMapValue(String key, String hKey, T value) {
        redisTemplate.opsForHash().put(key, hKey, value);
    }


    public <T> T getCacheMapValue(String key, String hKey) {
        return (T) redisTemplate.opsForHash().get(key, hKey);
    }

    public void delCacheMapValue(String key, String hKey) {
        redisTemplate.opsForHash().delete(key, hKey);
    }

    public <T> List<T> getMultiCacheMapValue(String key, Collection<Object> hKeys){
        return redisTemplate.opsForHash().multiGet(key, hKeys);
    }

    /**
     * 获得缓存的基本对象列表
     * @param pattern 字符串前缀
     * @return 对象列表
     */
    public  Collection<String> keys(String pattern){
        return redisTemplate.keys(pattern);
    }
}
